/**
 * CoffeeMilk (C) 2010 Amen Hamdan
 * 
 * See the NOTICE file distributed with this work for additional 
 * information regarding copyright ownership.  This file is licenses
 * to you under the Apache License, Version 2.0 (the "License"); you 
 * may not use this file except in compliance with the License.  You 
 * may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 *     
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 
 * implied. See the License for the specific language governing 
 * permissions and limitations under the License.    
 * 
 */
package ameen.rtm.lib.types;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;

import org.codehaus.jackson.JsonNode;

public class RtmDataFactory {
	private static final SimpleDateFormat fsDateFormatter = new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ssz" );

	public static RtmList createRtmList(JsonNode node) {
		RtmList rtmList = new RtmList();

		rtmList.setId(node.get("id").getTextValue());
		rtmList.setName(node.get("name").getTextValue());
		rtmList.setIsSmart(node.get("smart").getTextValue().equals("1"));

		//		rtmList.setDeleted(node.get("deleted").getBooleanValue());
		//		rtmList.setLocked(node.get("locked").getBooleanValue());

		return rtmList;
	}

	public static RtmTask createRtmTask(JsonNode node) {
		RtmTask rtmTask = new RtmTask();

		rtmTask.setTaskSeriesId(node.get("id").getTextValue());
		rtmTask.setName(node.get("name").getTextValue());

		rtmTask.setId(node.get("task").get("id").getTextValue());
		rtmTask.setPriority(node.get("task").get("priority").getTextValue());
		String dateString = node.get("task").get("due").getTextValue();
		if(dateString.equals("")) {
			rtmTask.setDueDate(null);
		} else {
			try {
				rtmTask.setDueDate(parseDate(dateString));
			} catch (ParseException e) {
				System.out.println(dateString);
				e.printStackTrace();
			}
		}
		
		if(node.get("task").get("completed").getTextValue().equals("")) {
			rtmTask.setCompleted(false);
		} else {
			rtmTask.setCompleted(true);
		}
		
		JsonNode tagsArray = node.get("tags").get("tag");
		if(tagsArray != null) {
			Vector<String> tagsVector = new Vector<String>();
			for(int i=0; i<tagsArray.size(); i++) {
				tagsVector.add(tagsArray.get(i).getTextValue());
			}
			rtmTask.setTags(tagsVector);
		}
	
		JsonNode notesArray = node.get("notes").get("note");
		if(notesArray != null) {
			NotesVector notesVector = new NotesVector();
			if(notesArray.isArray()) {
				for(int i=0; i<notesArray.size(); i++) {
					notesVector.add(createNote(notesArray.get(i)));
				}
			} else {
				notesVector.add(createNote(notesArray));
			}			rtmTask.setNotes(notesVector);
		}
		
		return rtmTask;
	}

	public static RtmTaskNote createNote(JsonNode noteNode) {
		String id = noteNode.get("id").getTextValue();
		String title = noteNode.get("title").getTextValue();
		String text = noteNode.get("$t").getTextValue();
		
		return new RtmTaskNote(id, title, text);
	}
	
    public static Date parseDate(String input) throws ParseException {

        if ( input.endsWith( "Z" ) ) {
            input = input.substring( 0, input.length() - 1) + "GMT-00:00";
        } else {
            int inset = 6;
            String start = input.substring(0, input.length() - inset );
            String end = input.substring(input.length() - inset, input.length() );
            input = start + "GMT" + end;
        }
        return fsDateFormatter.parse( input );
    }	
    public String toRtmDateString(Date date) {
    	return fsDateFormatter.format(date);
    }
	
}
